
//
//  YZQCollectionViewCell.m
//  SuperNews
//
//  Created by qiang on 16/1/11.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "YZQCollectionViewCell.h"
#import "YZQByTurnPicTableViewCell.h"
#import "YZQBigPictureTableViewCell.h"
#import "YZQBigPictureDownTableViewCell.h"
#import "YZQThreePicTableViewCell.h"

@interface YZQCollectionViewCell () <UITableViewDataSource, UITableViewDelegate, NumberDelegate>
@property (nonatomic, retain) UIImageView *imageV;
@end
@implementation YZQCollectionViewCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.pageCount = 0;
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, -25, [UIScreen mainScreen].bounds.size.width, (667 - 64)) style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    
    // 注册cell
    [self.tableV registerClass:[YZQNewsTableViewCell class] forCellReuseIdentifier:@"NewsTableViewCell"];
    [self.tableV registerClass:[YZQByTurnPicTableViewCell class] forCellReuseIdentifier:@"ByTurnsPictureTableViewCell"];
    [self.tableV registerClass:[YZQBigPictureTableViewCell class] forCellReuseIdentifier:@"BigPictureTableViewCell"];
    [self.tableV registerClass:[YZQBigPictureDownTableViewCell class] forCellReuseIdentifier:@"BigPictureDownTableViewCell"];
    [self.tableV registerClass:[YZQThreePicTableViewCell class] forCellReuseIdentifier:@"ThreePctureTableViewCell"];
    
    [self addSubview:self.tableV];
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsPageModel *newsM = [self.tableViewArr objectAtIndex:indexPath.row];
    
    // 第一个cell为
    if (0 == indexPath.row) {
 
        //轮播图
        if ([[[self.tableViewArr firstObject] ads] count] > 2) {
            
            YZQByTurnPicTableViewCell *ByTurnsPictureTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"ByTurnsPictureTableViewCell"];
            
            ByTurnsPictureTableViewCell.newsM = [self.tableViewArr firstObject];
            ByTurnsPictureTableViewCell.numberDele = self;
            return ByTurnsPictureTableViewCell;
            
            
        }else{
            // 正常大图
            YZQBigPictureTableViewCell *bigPictureTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"BigPictureTableViewCell"];
            bigPictureTableViewCell.newsM = newsM;
            return bigPictureTableViewCell;
        }
    }
    // 多图
    if (newsM.imgextra != nil) {
        YZQThreePicTableViewCell *threePictureDownTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"ThreePctureTableViewCell"];
        threePictureDownTableViewCell.newsM = newsM;
        return threePictureDownTableViewCell;
        
    }else if ([newsM.imgType integerValue] == 1) {// 大图
        
        YZQBigPictureDownTableViewCell *bigPictureDownTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"BigPictureDownTableViewCell"];
        bigPictureDownTableViewCell.newsM = newsM;
        return bigPictureDownTableViewCell;
        
    }
    
    // 普通的cell
    
    YZQNewsTableViewCell *newsTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"NewsTableViewCell"];
    newsTableViewCell.newsM = newsM;
    
    
    return newsTableViewCell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsPageModel *newsM = [self.tableViewArr objectAtIndex:indexPath.row];
    // 轮播图
    if (indexPath.row == 0) {
        return 200;
    }
    // 大图
    if (1 == [newsM.imgType integerValue]) {
        return 220;
    }
    
    // 多图
    if (newsM.imgextra != nil)
    {
        return  130;
        
    }
    // 正常
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.tableViewArr.count;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NewsPageModel *newsM = [self.tableViewArr objectAtIndex:indexPath.row];
    
    // 刚选中又马上取消选中，格子不变色
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:[tableView cellForRowAtIndexPath:indexPath] cache:YES];
        if (newsM.imgextra != nil) {
            [self.newsCollectionViewCelldelegate didSelectWithItem:newsM type:@"photos"];
        }else {
            [self.newsCollectionViewCelldelegate didSelectWithItem:newsM type:@"normal"];
            
        }
        
    }else
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:[tableView cellForRowAtIndexPath:indexPath] cache:YES];
        // 多图
        if (newsM.imgextra != nil) {
            [self.newsCollectionViewCelldelegate didSelectWithItem:newsM type:@"photos"];
        }else
            
            // 大图
            if ([newsM.imgType integerValue] == 1){
                [self.newsCollectionViewCelldelegate didSelectWithItem:newsM type:@"big"];
            }
            else{
                [self.newsCollectionViewCelldelegate didSelectWithItem:newsM type:@"normal"];
                
            }
    
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
    }];
    
}

- (void)sendNumber:(NSString *)number arr:(NSMutableArray *)arr
{
    self.number = number;
    [self.newsCollectionViewCelldelegate sendMessage:number arr:arr];
    
}


@end
